layui.define(['Router','Zutil','Router','laytpl','laypage'],function(exports) {
	var Zutil  =layui.Zutil;
	var Router = layui.Router;
	var laytpl = layui.laytpl;
	var laypage =layui.laypage;
	var Router = layui.Router;

	var obj = {};
	if(Zutil.getQueryString("fieldId"))obj.fieldId=Zutil.getQueryString("fieldId");
	if(Zutil.getQueryString("keyfrom"))obj.keyfrom=Zutil.getQueryString("keyfrom");
	if(Zutil.getQueryString("pattern"))obj.pattern=Zutil.getQueryString("pattern");
	if(Zutil.getQueryString("pageNumber"))obj.pageNumber = Zutil.getQueryString("pageNumber") ||1;
	if(Zutil.getQueryString("pageSize"))obj.pageSize =  Zutil.getQueryString("pageSize") || 10;
	if(Zutil.getQueryString("authors"))obj.authors = Zutil.decodeURI(window.atob(Zutil.getQueryString("authors") || "")); 
	if(Zutil.getQueryString("maxopenTime"))obj.maxopenTime = Zutil.decodeURI(window.atob(Zutil.getQueryString("maxopenTime")  || ""));
	if(Zutil.getQueryString("minopenTime"))obj.minopenTime =  Zutil.decodeURI(window.atob(Zutil.getQueryString("minopenTime")  || "")); 
	if(Zutil.getQueryString("keyword"))obj.keyword =  Zutil.decodeURI(window.atob(Zutil.getQueryString("keyword")  || ""));
	
	
	if(Zutil.getQueryString("timeliness")){
		if (Zutil.decodeURI(window.atob(Zutil.getQueryString("timeliness"))) == "全部") {
			obj.timeliness = ""
		}else{
			obj.timeliness =  Zutil.decodeURI(window.atob(Zutil.getQueryString("timeliness")))
		}
	}

	if(Zutil.getQueryString("mineffectiveTime"))obj.mineffectiveTime =  Zutil.decodeURI(window.atob(Zutil.getQueryString("mineffectiveTime")  || ""));
	if(Zutil.getQueryString("maxeffectiveTime"))obj.maxeffectiveTime =Zutil.decodeURI(window.atob( Zutil.getQueryString("maxeffectiveTime")  || "")); 
	if(Zutil.getQueryString("key"))obj.key = Zutil.decodeURI(window.atob( Zutil.getQueryString("key") || ""));
	if(Zutil.getQueryString("fq"))obj.fq = Zutil.decodeURI(window.atob( Zutil.getQueryString("fq") || ""));
	if(Zutil.getQueryString("fqkeyfrom"))obj.fqkeyfrom=Zutil.decodeURI(window.atob( Zutil.getQueryString("fqkeyfrom") || ""));
	if(Zutil.getQueryString("fqauthors"))obj.fqauthors=Zutil.decodeURI(window.atob( Zutil.getQueryString("fqauthors") || ""));
	
	$("#key").val(obj.key);
	$("input:radio[name='keyfrom'][value='"+obj.keyfrom+"']").attr("checked","checked");
	$("input:radio[name='pattern'][value='"+obj.pattern+"']").attr("checked","checked");
	$("#authors").val(obj.authors);
	$("#minopenTime").val(obj.minopenTime);
	$("#maxopenTime").val(obj.maxopenTime);
	$("#keyword").val(obj.keyword);
	$("#timeliness").html(obj.timeliness);
	$("#mineffectiveTime").val(obj.mineffectiveTime);
	$("#maxeffectiveTime").val(obj.maxeffectiveTime);
	if(obj.authors || obj.minopenTime || obj.maxopenTime || obj.keyword  || obj.timeliness || obj.mineffectiveTime || obj.maxeffectiveTime ) {
		$(".searchHide").show();
	}
	getList(obj);
	
	
	
	$("#key").on("keydown",function (event){
		e = event ? event :(window.event ? window.event : null);
		
		if (e.keyCode==13) {
			var search = {};
			obj.keyfrom=$("input:radio[name='keyfrom']:checked").val();
		    obj.pattern=$("input:radio[name='pattern']:checked").val();
			obj.authors = $("#authors").val();
			obj.maxopenTime = $("#maxopenTime").val();
			obj.minopenTime =$("#minopenTime").val();
			obj.keyword =$("#keyword").val();
			obj.timeliness =$("#timeliness").text() == "全部"?"":$("#timeliness").text();
			obj.mineffectiveTime =$("#mineffectiveTime").val();
			obj.maxeffectiveTime =$("#maxeffectiveTime").val();
			obj.key = $("#key").val();
			delete obj.fieldId;
			delete obj.fqauthors;
			delete obj.fq;
			delete obj.fqkeyfrom;
			makeUrl();
			}
		})
	
	
	$(".searchBtn").click(function(){
		var search = {};
		obj.keyfrom=$("input:radio[name='keyfrom']:checked").val();
	    obj.pattern=$("input:radio[name='pattern']:checked").val();
		obj.authors = $("#authors").val();
		obj.maxopenTime = $("#maxopenTime").val();
		obj.minopenTime =$("#minopenTime").val();
		obj.keyword =$("#keyword").val();
		obj.timeliness =$("#timeliness").text() == "全部"?"":$("#timeliness").text();
		obj.mineffectiveTime =$("#mineffectiveTime").val();
		obj.maxeffectiveTime =$("#maxeffectiveTime").val();
		obj.key = $("#key").val();
		delete obj.fieldId;
		delete obj.fqauthors;
		delete obj.fq;
		delete obj.fqkeyfrom;
		makeUrl();
	});
	
	$(".searchBtn1").click(function(){
		var search = {};
	    obj.pattern=$("input:radio[name='pattern']:checked").val();
		obj.authors = $("#authors").val();
		obj.maxopenTime = $("#maxopenTime").val();
		obj.minopenTime =$("#minopenTime").val();
		obj.keyword =$("#keyword").val();
		obj.timeliness =$("#timeliness").text() == "全部"?"":$("#timeliness").text();
		obj.mineffectiveTime =$("#mineffectiveTime").val();
		obj.maxeffectiveTime =$("#maxeffectiveTime").val();
		obj.fqkeyfrom = obj.keyfrom;
		obj.fq = obj.key;
		obj.keyfrom=$("input:radio[name='keyfrom']:checked").val();
		obj.key = $("#key").val();
		delete obj.fieldId;
		delete obj.fqauthors;
		makeUrl();
	});
	
	
	function makeUrl(pageNumber,pageSize){
		var searchUrl ="/ceca_two/modules/policy/policyList.html?";
		if(obj.fieldId)searchUrl+="&fieldId="+obj.fieldId;
		if(obj.keyfrom)searchUrl+="&keyfrom="+obj.keyfrom;
		if(obj.pattern)searchUrl+="&pattern="+obj.pattern;
		if(pageNumber)searchUrl+="&pageNumber="+obj.pageNumber;
		if(pageSize)searchUrl+="&pageSize="+obj.pageSize; 
		if(obj.authors)searchUrl+="&authors="+window.btoa(Zutil.encodeURI(obj.authors || ""));
		if(obj.maxopenTime)searchUrl+="&maxopenTime="+window.btoa(Zutil.encodeURI(obj.maxopenTime || ""));
		if(obj.minopenTime)searchUrl+="&minopenTime="+window.btoa(Zutil.encodeURI(obj.minopenTime|| "") );
		if(obj.keyword)searchUrl+="&keyword="+window.btoa(Zutil.encodeURI(obj.keyword || ""));
		if(obj.timeliness)searchUrl+="&timeliness="+window.btoa(Zutil.encodeURI(obj.timeliness|| ""));
		if(obj.mineffectiveTime)searchUrl+="&mineffectiveTime="+window.btoa(Zutil.encodeURI(obj.mineffectiveTime|| "" ));
		obj.key= $("#key").val();
		if(obj.key)searchUrl+="&key="+window.btoa(Zutil.encodeURI(obj.key || ""));
		if(obj.fq)searchUrl+="&fq="+window.btoa(Zutil.encodeURI(obj.fq || ""));
		if(obj.fqkeyfrom)searchUrl+="&fqkeyfrom="+window.btoa(Zutil.encodeURI(obj.fqkeyfrom || ""));
		if(obj.fqauthors)searchUrl+="&fqauthors="+window.btoa(Zutil.encodeURI(obj.fqauthors || ""));
		window.location.href=searchUrl;
	}
	
	
	function getList(o){
		var search=Router.link.costnorm.search;
		if(Zutil.getQueryString("policiesId"))return false;
		$.post(search,o,function(result){
			var  data = result.data;
			laytpl(AllcontentListText.innerHTML).render(data.list, function(html){	
				AllcontentList.innerHTML = html;
			});
			for (i = 0 ; i < data.list.length ; i++){
				var s = data.list[i].abstract ? data.list[i].abstract.toString():"";
				$(".contentListP").eq(i).html(s);
			}
			for (i = 0 ; i < $(".fwjg").length ; i++) {
				var str = $(".fwjg").eq(i).html();
				str == "null" ? str = " #0":"";
				$(".fwjg").eq(i).html("");
				var arr = str.split(";");
				var json = {};
				var num;
				arr.length == 1 ? num1 = 1 : num1 = arr.length-1 ;
			    for(var j = 0 ; j < num1 ; j++){
			        var name = arr[j].match(/[^#]+(?=[#])/g)? arr[j].match(/[^#]+(?=[#])/g)[0] :arr[j] ;
         			var num  =  arr[j].match(/[^#]+(?=[#])/g)? arr[j].match(/#(\S*)/)[1] :0;
			        json[name] = num;
			    }
			    var arr2 = [];
			    for(name in json){
		        	arr2.push({"name":name,"num":json[name]})
		   		}
			    arr2.sort(function (a,b){
			    	return a.num-b.num;
			    })
				for(k = 0 ; k < arr2.length ; k++){
					$(".fwjg").eq(i).append("<i style='color: #333;font-weight: normal;width:auto;margin-right:10px'>"+arr2[k].name+"</i>")
				}
			}
			laypage({
			    cont: 'laypage'
			    ,pages: data.totalPage
			    ,curr:data.pageNumber
			    ,groups:data.pageSize
			    ,skin: '#ff0000'
			    ,jump: function(opt, first){
				    var curr = opt.curr;   //得到了当前页，用于向服务端请求对应数据
				    obj.pageNumber = curr|| 1;
				    obj.pageSize = data.pageSize|| 10;
				    if(!first)makeUrl(obj.pageNumber,obj.pageSize);
				  }
			});
		});
	}
	var n = 0;
	var Router = layui.Router;
	var getField=Router.link.costnorm.getField;
	$(".searchMore").click(function(){
		if($(".searchHide").css("display")=="none"){
			$(".searchHide").show();
			$(".searchMore span").addClass('searchMoreIcoZ');
		}else{
			$(".searchMore span").removeClass('searchMoreIcoZ');
			$(".searchHide").hide();
		}
	});
	var list={
		data:{},
		reptain:function(d,fid){
		    var reData = d || this.data;
			$(".xialai").empty();
			for (i = 0 ; i < reData.length ; i++) {
				if(reData[i].fieldParantId == 1 && fid == undefined){
					$(".xialai").append("<dd style='display:block'><a onclick='$(\".listbox\").show();$(\".ifbox\").hide();$(\".xhwj\").remove()'  data-fieldId="+reData[i].fieldId+"  href=\"javascript:;\">"+reData[i].fieldName+"</a></dd>");
				}
				if (reData[i].fieldId == fid) {
					$(".tit-desc").html(reData[i].fieldName+"<span class='layui-nav-more'></span>")
//					$(".shareList").append("<li>></li><li><a onclick='layui.Policylist.filter("+reData[i].fieldId+");'>"+reData[i].fieldName+"</a></li>")
				}
				if(fid == reData[i].fieldParantId){
					$(".xialai").append("<dd style='display:block'><a   onclick='$(\".listbox\").show();$(\".ifbox\").hide();$(\".xhwj\").remove()'  data-fieldId="+reData[i].fieldId+"  href=\"javascript:;\">"+reData[i].fieldName+"</a></dd>")					
				}
			}
			
//			if(window.location.href.indexOf("policyDetails")!=-1){
//				$(".shareList li").last().empty();
//				$(".shareList").append("<li class='xhwj'>协会文件</li>")
//			}
		},
		getter:function(){
			var field =sessionStorage.getItem("field");
			if(field == null || field =="null"){
				$.post(getField,{},function(result){
					list.data= result.data;
					var  data = result.data;
					getNavTree(data)
				});
			}else{
				list.data= JSON.parse(field);
				var  data = list.data;
				getNavTree(data)
			}
		},
		filter:function(fieldId){
			if(fieldId){
				var newfield = new Array();
				var arr = [];
				for ( i = 0 ; i < list.data.length ; i++) {
					if(list.data[i].fieldId.startsWith(fieldId)) arr.push(list.data[i]);
				}
				if(arr.length == 1 || arr.length == 0){
					var data = $(this)[0].data;
					for (i = 0 ; i < data.length ; i++) {
						if (data[i].fieldId == fieldId) {
							$(".xialai").empty();
							$(".xialai").append("<dd class='xz' data-fieldid='"+data[i].fieldId+"'>"+data[i].fieldName+"</dd>")
							$(".xz").css({"color":"#fff","background":"rgba(255,0,26,0.4)","width":"144px","margin-left":"40px","height":"30px","line-height":"30px","width":"170px","padding-left":"15px"})
//							$(".shareList").append("<li>></li><li><a onclick='layui.Policylist.filter("+data[i].fieldId+");'>"+data[i].fieldName+"</a></li>")
							obj.pageNumber = obj.pageNumber|| 1;
							obj.pageSize = obj.pageSize || 10;
							obj.authors = data[i].fieldName;
							makeUrl();
						}
					}
				}else{
					this.reptain(arr,fieldId);
				}
			}else{
				this.reptain();
			}
//			$(".shareList a").on("click",function (){
//				$(this).parent("li").nextAll().remove();
//				$(".listbox").show();
//				$(".ifbox").hide();
//			});
	},
	menuClick:function(fieldId){
	}
}
$(".tit-desc").on("click",function (){
	$(".layui-nav-child").toggleClass("xialai")
})
$("#timeliness").on("click",function (){
	$(this).next(".seceltTextUl").toggle();
})

function getNavTree (data){
	laytpl(menuItem.innerHTML).render(data, function(html){
		fieldTree.innerHTML = html;
		var thisUrl = window.location.href ;
		
		
		
		if(thisUrl.indexOf(10004) != -1){
			$("#fieldTree li").eq(0).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10004'>"+$("#fieldTree li").eq(0).find("a").first().html()+"</a></li>")

		}else if (thisUrl.indexOf(10003) != -1){
			$("#fieldTree li").eq(1).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=100053'>"+$("#fieldTree li").eq(1).find("a").first().html()+"</a></li>")
		}else if (thisUrl.indexOf(10005) != -1){
			$("#fieldTree li").eq(2).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10005'>"+$("#fieldTree li").eq(2).find("a").first().html()+"</a></li>")
		}else if (thisUrl.indexOf(19001) != -1){
			$("#fieldTree li").eq(3).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10001'>"+$("#fieldTree li").eq(3).find("a").first().html()+"</a></li>")
		}else if (thisUrl.indexOf(10000) != -1){
			$("#fieldTree li").eq(4).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10000'>"+$("#fieldTree li").eq(4).find("a").first().html()+"</a></li>")
		}else if (thisUrl.indexOf(10002) != -1){
			$("#fieldTree li").eq(5).addClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10002'>"+$("#fieldTree li").eq(5).find("a").first().html()+"</a></li>")
		}
	
		
		for (i = 0 ; i < $("#fieldTree .layui-nav-itemed a").length ; i++) {
			var thisfieldid = $("#fieldTree .layui-nav-itemed a").eq(i).attr("data-fieldid");
			
			if (thisUrl.indexOf(thisfieldid) != -1) {
				$("#fieldTree .layui-nav-itemed a").eq(i).addClass("active");
				$(".shareList").append("<li>></li><li><a href='/ceca_two/modules/policy/policyList.html?&fieldId=10000'>"+$("#fieldTree .layui-nav-itemed a").eq(i).html()+"</a></li>")
				
			}
			
		}
	
		$("#fieldTree > li > a").on("click",function (){
			
			if ($(this).parent("li").hasClass("layui-nav-itemed")) {
				$(this).parent("li").removeClass("layui-nav-itemed");
			}else{
				$("#fieldTree > li").not($(this).parent("li")).removeClass("layui-nav-itemed");
				$("#fieldTree > li dd").removeClass("layui-this");
				$(this).parent("li").toggleClass("layui-nav-itemed");
				obj.fieldId = $(this).attr("fieldId");
				delete obj.fqauthors; 
				makeUrl();
			}
			
		});
		$("#fieldTree > li dd").click(function(){
			var fieldId =$(this).find("a").attr("data-fieldId") ||Zutil.getQueryString("fieldId");

//			$("#wordsec").show().children("a").html();
//			$("#smallsec").show().children("a").html($(this).text());
			$("#fieldTree > li dd").removeClass("layui-this");
			$(this).toggleClass("layui-this");
			obj.fieldId=fieldId;
			obj.fqauthors = $(this).text(); 
			makeUrl();
			return false;
		});
	});
}
	list.getter();
	exports('Policylist', list); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});
